package com.lovo.product.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.lovo.common.bean.Product;
import com.lovo.common.result.ResponseResult;
import com.lovo.product.form.ProductForm;
import com.lovo.product.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.events.Event;

@RestController
@RequestMapping("/products")
@Tag(name = "商品管理ProductController")
@SecurityRequirement(name = "bearer-key")
public class ProductController {
    @Autowired
    private ProductService productService;
    @GetMapping("/ids")
    @Operation(summary = "根据多个id获取商品")
    @SentinelResource("getByIds")
    public Object getByIds(ProductForm productForm){
        System.out.println("ids:" + productForm.getIds());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return productService.getBaseMapper().selectBatchIds(productForm.getIds());
    }
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取商品")
    @ResponseResult
    public Object getById(@PathVariable Integer id){
        return productService.getById(id);
    }
    @PutMapping("/{id}")
    @Operation(summary = "修改商品")
    @ResponseResult
    public Object update(@PathVariable Integer id,@RequestBody ProductForm productForm){
        Product product = new Product();
        BeanUtil.copyProperties(productForm,product,true);
        product.setId(id);
        productService.updateById(product);
        return null;
    }


}
